const DynamicProgramming = require("../template")

function testMatrixProblems() {
  const dp = new DynamicProgramming()

  console.log(
    "1. 矩阵路径问题基础测试:",
    (() => {
      return dp.uniquePaths(3, 7) === 28 &&
             dp.uniquePaths(3, 2) === 3 &&
             dp.uniquePaths(7, 3) === 28
    })()
  )

  console.log(
    "2. 矩阵路径问题边界测试:",
    (() => {
      return dp.uniquePaths(3, 3) === 6 &&
             dp.uniquePaths(1, 1) === 1 &&
             dp.uniquePaths(1, 10) === 1 &&
             dp.uniquePaths(10, 1) === 1
    })()
  )

  console.log(
    "3. 最小路径和基础测试:",
    (() => {
      const grid1 = [[1, 3, 1], [1, 5, 1], [4, 2, 1]]
      const grid2 = [[1, 2, 3], [4, 5, 6]]
      return dp.minPathSum(grid1.map(row => [...row])) === 7 &&
             dp.minPathSum(grid2.map(row => [...row])) === 12
    })()
  )

  console.log(
    "4. 最小路径和单元素测试:",
    (() => {
      const grid = [[1]]
      return dp.minPathSum(grid.map(row => [...row])) === 1
    })()
  )

  console.log(
    "5. 最小路径和2x2矩阵测试:",
    (() => {
      const grid = [[1, 2], [1, 1]]
      return dp.minPathSum(grid.map(row => [...row])) === 3
    })()
  )

  console.log(
    "6. 最小路径和复杂矩阵测试:",
    (() => {
      const grid = [
        [5, 0, 1, 1, 2, 1, 0, 1, 3, 6, 3, 0, 7, 3, 3, 3, 0],
        [4, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
      ]
      return dp.minPathSum(grid.map(row => [...row])) === 7
    })()
  )

  console.log(
    "7. 矩阵路径边界情况测试:",
    (() => {
      return dp.uniquePaths(0, 0) === 0 &&
             dp.uniquePaths(0, 1) === 0 &&
             dp.uniquePaths(1, 0) === 0
    })()
  )

  console.log(
    "8. 最小路径和边界情况测试:",
    (() => {
      return dp.minPathSum([]) === 0 &&
             dp.minPathSum([[1, 2, 3]]) === 6 &&
             dp.minPathSum([[1], [2], [3]]) === 6
    })()
  )

  console.log(
    "9. 大矩阵性能测试:",
    (() => {
      const largeGrid = Array.from({ length: 10 }, (_, i) =>
        Array.from({ length: 10 }, (_, j) => Math.floor(Math.random() * 10) + 1)
      )
      const start = performance.now()
      const result = dp.minPathSum(largeGrid.map(row => [...row]))
      const end = performance.now()
      return (end - start) < 1000 && result > 0 // 应该在1秒内完成且结果为正数
    })()
  )

  console.log(
    "10. 全零矩阵测试:",
    (() => {
      const specialGrid = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
      return dp.minPathSum(specialGrid.map(row => [...row])) === 0
    })()
  )

  console.log(
    "11. 包含负数矩阵测试:",
    (() => {
      const negativeGrid = [[1, -1, 1], [-1, 1, -1], [1, -1, 1]]
      return dp.minPathSum(negativeGrid.map(row => [...row])) === -1
    })()
  )

  console.log(
    "12. this上下文矩阵测试:",
    (() => {
      const matrixObj = {
        multiplier: 3,
        processMatrix: function(m, n) {
          const dp = new DynamicProgramming()
          const result = dp.uniquePaths(m, n)
          return result * this.multiplier
        }
      }
      return matrixObj.processMatrix(2, 2) === 6 // 2 * 3 = 6
    })()
  )

  console.log(
    "13. 复杂对象this绑定测试:",
    (() => {
      const matrixConfigObj = {
        config: { offset: 5 },
        processWithConfig: function(grid) {
          const dp = new DynamicProgramming()
          const result = dp.minPathSum(grid)
          return result + this.config.offset
        }
      }
      const testGrid = [[1, 2], [3, 4]]
      return matrixConfigObj.processWithConfig(testGrid.map(row => [...row])) === 10 // 5 + 5 = 10
    })()
  )
}

testMatrixProblems()
